package com.controller;

import com.bean.User;
import com.mapper.GoodMapper;
import com.service.GoodService;
import com.service.PostService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : UserController
 * @Auther : acerolan@foxmail.com
 * @Data : 2024/9/9 17:11
 * @Description :
 */
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    GoodService goodService;

    @Autowired
    PostService postService;

    @GetMapping("/getUser")
    public Map<String, Object> getUser(@RequestParam("userId") int userId) {

        Map<String, Object> userInfo = userService.getUserInfoById(userId);

        return userInfo;
    }

    @PostMapping("/getUserInfo")
    public Map<String, Object> getUserInfo(
            @RequestBody Map<String, Object> loginMap) {

        Map<String, Object> map = new HashMap<>();
        map.put("phone_number", loginMap.get("account"));
        map.put("password", loginMap.get("password"));

        int userId = userService.getUserIdByMap(map);
        System.out.println(userId);
        if (userId == 0) {
            return null;
        } else {
            Map<String, Object> userInfo = userService.getUserInfoById(userId);
            return userInfo;
        }
    }

    @GetMapping("/user/getPageOwnerInfoByGoodId")
    public Map<String, Object> getPageOwnerInfoByGoodId(@RequestParam("goodId") int goodId) {

        return goodService.getPageOwnInfoByGoodId(goodId);
    }

    @GetMapping("/user/getPageOwnerInfoByPostId")
    public Map<String, Object> getPageOwnerInfoByPostId(@RequestParam("postId") int postId) {

        return postService.getPageOwnInfoByPostId(postId);
    }

    @GetMapping("/user/getPageOwnerInfoByUserId")
    public Map<String, Object> getPageOwnerInfoByUserId(@RequestParam("userId") int userId) {

        return userService.getSimpleUserInfoById(userId);
    }

    @PostMapping("/zhuce")
    public int zhuce(@RequestBody Map<String, Object> zhuceMap) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone_number", zhuceMap.get("account"));
        int userId = userService.getUserIdByMap(map);
        if (userId != 0) { // 手机号已注册
            return -1;
        }
        // 手机号未注册则
        User user = new User();
        user.setNickName("用户" + zhuceMap.get("account"));
        user.setPhoneNumber(zhuceMap.get("account") + "");
        user.setPassword(zhuceMap.get("password") + "");

        user.setIcon("img%2Fusers%2Fdefault%2Fdefault.jpg");

        return userService.zhuCeUser(user);
    }

}
